package com.csoa.community.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.csoa.community.requests.AbstractRequest;
import com.csoa.community.singletons.GlobalVars;
import com.csoa.community.utils.AndroidUtils;

public class BaseActivity extends Activity {

	protected TextView mCommonButton;
	protected ImageView mCommonLogo;
	protected TextView mCommonTitle;
	public static BaseActivity _activity;
	protected Handler mHandler = new Handler();
	private ProgressDialog progressDialog;
	public static List<BaseActivity> activities = null;

	public void hideCommonTitle() {
		if ((mCommonTitle != null) && (mCommonTitle.getVisibility() == 0))
			mCommonTitle.setVisibility(4);
	}

	protected void hideInputMethod(View paramView) {
		if (paramView != null) {
			((InputMethodManager) getSystemService("input_method"))
					.hideSoftInputFromWindow(paramView.getWindowToken(), 2);
		}
		getWindow().setSoftInputMode(3);
	}

	public void initData() {
	}

	public void initUI() {
	}

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		_activity = this;

		if (this instanceof MainActivity) {
			if (activities == null) {
				activities = new ArrayList<BaseActivity>();
			}
			activities.add(this);
		}

		initUI();
		initData();
		hideInputMethod(null);
	}

	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
		if ((paramInt == 4) && (paramKeyEvent.getRepeatCount() == 0)) {
			finish();
			super.onKeyDown(paramInt, paramKeyEvent);
		}
		return false;
	}

	protected void onPause() {
		super.onPause();
	}

	protected void onResume() {
		super.onResume();
		adjustScreenBright();
		

	}
	
	public void adjustScreenBright() {
		String screenBright = AndroidUtils.getGlobal(GlobalVars.LOGIN,
				GlobalVars.LOGIN_CURRENT_SCREEAN_BRIGHT, this);
		if (screenBright != null && "1".equals(screenBright)) {
			AndroidUtils.adjustScreenBright(this);
		} else {
			AndroidUtils.adjustScreenBright(this, -1);
		}
	}

	public void showCommonButton(int paramInt,
			View.OnClickListener paramOnClickListener) {
	}

	public void showCommonButton(String paramString,
			View.OnClickListener paramOnClickListener) {
	}

	public void showCommonLogo() {
		if (mCommonLogo == null)
			mCommonLogo = ((ImageView) findViewById(2131296331));
		if ((mCommonLogo != null) && (mCommonLogo.getVisibility() != 0))
			mCommonLogo.setVisibility(0);
	}

	public void showCommonTitle(String paramString) {
		if ((mCommonTitle != null) && (mCommonTitle.getVisibility() != 0)) {
			mCommonTitle.setVisibility(0);
		}
	}

	public void showToast(final String content) {
		Toast localToast = Toast.makeText(BaseActivity.this, content,
				Toast.LENGTH_LONG);
		localToast.show();
	}

	public void showProgress() {
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(this);
		}
		if (!progressDialog.isShowing()) {
			progressDialog.setMessage("正在加载数据,请稍后..");
			progressDialog.setCancelable(true);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.show();
		}
	}

	public void dismisProgress() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	public interface OnUICallback {
		public void onGetResult(AbstractRequest result);

		public void onCancel();

		void onProgress(int progress, String msg);
	}

	public void closeAllActivity() {
		if (activities != null) {
			for (int i = 0; i < activities.size(); i++) {
				activities.get(i).finish();
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		dismisProgress();
	}

	// @Override
	// public void startActivity(Intent intent) {
	// intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
	// super.startActivity(intent);
	// }
	//
	// @Override
	// public void startActivityForResult(Intent intent, int requestCode) {
	// intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
	// super.startActivityForResult(intent, requestCode);
	// }
}
